package com.gxcc3.ygps.system.service.impl;

import com.gxcc3.ygps.common.constant.Constants;
import com.gxcc3.ygps.common.core.domain.entity.SysIotStorage;
import com.gxcc3.ygps.common.exception.ServiceException;
import com.gxcc3.ygps.common.utils.StringUtils;
import com.gxcc3.ygps.system.domain.SysConfig;
import com.gxcc3.ygps.system.mapper.SysIotStorageMapper;
import com.gxcc3.ygps.system.service.ISysIotStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysIotStorageServiceImpl implements ISysIotStorageService {

    @Resource
    SysIotStorageMapper storageMapper;


    /**
     * 获取冷库列表
     * @param storage 查询参数
     * @return 查询结果
     */
    @Override
    public List<SysIotStorage> selectStorageList(SysIotStorage storage) {
        return storageMapper.selectStorageList(storage);
    }

    /**
     * 新增冷库
     * @param storage 冷库
     * @return 插入结果
     */
    @Override
    public int insertStorage(SysIotStorage storage) {
        return storageMapper.insertStorage(storage);
    }


    /**
     * 获取冷库信息
     * @param id 主键
     * @return 结果
     */
    @Override
    public SysIotStorage selectStorageById(Long id) {
        return storageMapper.selectStorageById(id);
    }


    /**
     * 修改冷库信息
     * @param storage 冷库
     * @return 结果
     */
    @Override
    public int updateStorage(SysIotStorage storage) {
        return storageMapper.updateStorage(storage);
    }


    /**
     * 删除冷库
     * @param ids ids
     * @return 结果
     */
    @Override
    @Transactional
    public int delStorage(Long[] ids) {
        for (Long id : ids) {
            SysIotStorage storage = storageMapper.selectStorageById(id);
            if (StringUtils.equals(Constants.YGPS_DEVICE_REPORT_STATUS,storage.getReportStatus())){
                throw new ServiceException(String.format("%1$s 已上报,请先取消上报后删除", storage.getStorageName()));
            }
        }
        return storageMapper.delStorage(ids);
    }
}
